<?php
/**
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/10/16
 * Time: 10:54
 */

namespace app\admin\behavior;

use app\admin\model\Admin;
use app\admin\model\Power;
use app\admin\model\Role;
use think\Controller;

/**
 * 权限检测类:检测操作权限
 * Class CheckPermissions
 * @package app\admin\behavior
 */
class CheckPermissions extends Controller
{
    public function run()
    {
        $this->actionBegin();
    }

    /**
     * 整体思路，只要做了权限控制，就必须有该权限才能够访问，如果没有做权限控制的 就可以访问
     */
    public function actionBegin()
    {
        $m = request()->module();
        $c = request()->controller();
        $a = request()->action();
        $currUrl = strtolower("/" . $m . "/" . $c . "/" . $a);//当前访问路径
        $userinfo = getCurrLoginSession();
        $isSupperManager = isSupperManagerForSession();
        $allPowerList = $userinfo['systemAllPower'];//系统所有权限详细数组
        $systemAllPowerPath = $userinfo['systemAllPowerPath'];//系统所有权限PATH数组
        $currPowerList = $userinfo['CurrPower'];//当前权限

        if ($isSupperManager == 0) {//不是超级管理员权限
            if (in_array($currUrl, $systemAllPowerPath))//当前地址在所有需要判断权限的地址范围内
            {
                //把前端地址转换成权限ID
                $currPowerID = 0;//当前url对应的权限ID
                if (isset($allPowerList[$currUrl]) && !empty($allPowerList[$currUrl])) {
                    $currPowerID = $allPowerList[$currUrl]['id'];
                }
                if (empty($currPowerID)) {
                    echo '对不起你无权访问该功能';
                    exit;
                }
                if (!in_array($currPowerID, $currPowerList)) {
                    echo '对不起你无权访问该功能';
                    exit;
                }
            }
        }
    }
}